$(window).load(function(){
	/* 获取所选保险公司，渲染DOM结构 */
	var resultListMsg = JSON.parse(localStorage.getItem("eBizChoiceCompanyMsg"));
	console.log(resultListMsg);
	if(resultListMsg){
		for(var i=0;i<resultListMsg.length;i++){
			var resultCompanyLi = $('<li class="isflexbox"><div class="result-left isflexbox"><img src="'+resultListMsg[i].insurername+'" /><p>'+resultListMsg[i].imagename+'</p></div><div class="result-right isflexbox insurercode-'+resultListMsg[i].insurercode+'"><p><img src="images/result_loading.gif" /><span>正在报价</span></p><img class="result-arrow" src="images/right_arrow.png" /></div></li>');
			resultCompanyLi.appendTo($(".result-list ul"));
		}
	}



	/* 获取各个保险公司报价，渲染DOM结构 */
	var failureCompanyLen = 0;
	var postSimulate = wxAddress + "/weixin/quote/simulate"
	var resultListName = JSON.parse(localStorage.getItem("eBizChoiceCompanyList"));
	if(resultListName){
		for(var i=0;i<resultListName.length;i++){
			var simulateData = {
				insurerCode: resultListName[i]
			}
			ajaxEncapsulation(postSimulate, postSimulateSuc, "", "POST", "true", simulateData);
		}
	}

	var resultWholeArr = new Array();
	function postSimulateSuc(data, simulateData){
		console.log(data);
		var resultWholeMsg = data.data;
		var resultDataObj = new Object();
		var targetSelector = ".insurercode-"+simulateData.insurerCode;
		if(data.code == "0000"){
			if(resultWholeMsg.data == null){
				failureCompanyLen++;
				$(targetSelector).parent("li").hide();
				var failureImg = $(targetSelector).siblings(".result-left").find("img").attr("src");
				var failureName = $(targetSelector).siblings(".result-left").find("p").text();
				var resultFailureLi = $('<li><div class="isflexbox"><img src="'+failureImg+'" /><p>'+failureName+'</p></div><p class="failure-reason">【报价】报价失败：'+resultWholeMsg.msg+'</p></li>');
				resultFailureLi.appendTo($(".failure-list ul"));
			}else{
				$(targetSelector).html('<p><span>¥'+resultWholeMsg.totalPremium+'起</span></p><img class="result-arrow" src="images/right_arrow.png" />');
				var resultDataObjValue = resultWholeMsg.data[0].insurerCode;
				resultDataObj[resultDataObjValue] = resultWholeMsg;
				resultWholeArr.push(resultDataObj);
				console.log(resultWholeArr);
			}
		}else{
			failureCompanyLen++;
			$(targetSelector).parent("li").hide();
			var failureImg = $(targetSelector).siblings(".result-left").find("img").attr("src");
			var failureName = $(targetSelector).siblings(".result-left").find("p").text();
			var resultFailureLi = $('<li><div class="isflexbox"><img src="'+failureImg+'" /><p>'+failureName+'</p></div><p class="failure-reason">【报价】报价失败：'+data.msg+'</p></li>');
			resultFailureLi.appendTo($(".failure-list ul"));
		}
		if(failureCompanyLen != 0){
			$(".result-failure").removeAttr("style");
			$(".failure-title").text("未报价公司（"+failureCompanyLen+"家）");
		}
	}


	
	/* 选择保险公司，点击进入订单，存储数据 */
	$(".result-list ul li").delegate(".result-right", "click", function(){
		var clickCompany = $(this).attr("class").split("insurercode-")[1];
		var clickImage = $(this).siblings(".result-left").find("img").attr("src");
		var clickName = $(this).siblings(".result-left").find("p").text();
		for(var i=0;i<resultWholeArr.length;i++){
			for(var clickInsurercode in resultWholeArr[i]){
				if(clickInsurercode == clickCompany) {
					var clickCompanyMsg = {
						name: clickName,
						image: clickImage
					}
					window.localStorage.setItem("eBizOrderCompany", JSON.stringify(clickCompanyMsg));
					window.localStorage.setItem("eBizOrderMsg", JSON.stringify(resultWholeArr[i]));
					var unPostValue = JSON.parse(localStorage.getItem("eBizUnderWriteValue"));
					if(!unPostValue){
						unPostValue = new Object();
					}
					unPostValue.bizID = resultWholeArr[i][clickCompany].data[0].bizID;
					unPostValue.insurerCode = clickCompany;
					window.localStorage.setItem("eBizUnderWriteValue", JSON.stringify(unPostValue));
					window.location.href = "bizorder.html";
					return false;
				}
			}
		}
	})



	/* 未报价公司显示隐藏 */
	$(".result-reason").on("click", function(){
		$(".result-isoffer").hide();
		$(".result-unoffer").show();
		$("title").text("未报价公司");
	})

	$(".return-result").on("click", function(){
		$(".result-unoffer").hide();
		$(".result-isoffer").show();
		$("title").text("报价结果");
	})
})